package cn.b.sky.mes.pp.controller;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.common.model.Page;
import cn.b.sky.mes.pp.model.ProductionConfirm;
import cn.b.sky.mes.pp.model.ProductionOperation;
import cn.b.sky.mes.pp.model.condition.ProductionOperationCondition;
import cn.b.sky.mes.pp.service.ProductionConfirmDetailService;
import cn.b.sky.mes.pp.service.ProductionOrderConfirmService;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: lidapeng
 * @Description: 生产报工
 * @Date Created in 9/6/23 4:18 PM
 * @Modified By: lidapeng
 * Copyright(c) dbappsecurity.com.cn
 */
@Controller
@Slf4j
@RequestMapping(value = "/productionconfirm/*")
public class ProductionConfirmController extends SecurityController {

    @Autowired
    private ProductionOrderConfirmService productionOrderConfirmService;

    @Autowired
    private ProductionConfirmDetailService productionConfirmDetailService;


    @RequestMapping(value = "confirm_list")
    public String productionOperation(String navTabId, Model model,
                                      @ModelAttribute("condition") ProductionOperationCondition condition) {
        condition.setSiteCode(this.getSiteCode());
        Page page = productionOrderConfirmService.getProductionOperationByCondition(condition);
        model.addAttribute(NAVTABID, navTabId);
        model.addAttribute("page", page);
        return "pp/productionConfirm/confirm_index";

    }

    @RequestMapping(value = "confirm")
    public String confirmProductionOperation(String navTabId, Model model, Long id) {
        model.addAttribute(NAVTABID, navTabId);
        model.addAttribute("productionOperation", productionOrderConfirmService.getProductionOperationById(id));
        return "pp/productionConfirm/confirm";

    }

    @PostMapping(value = "saveConfirm")
    @ResponseBody
    public DWZResult saveProductionOperationConfirms(String navTabId, Model model, ProductionConfirm confirm) {
        if (confirm == null) {
            return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
        }
        this.setCreateInfo(confirm);
        confirm.setConfirmDateTime(new Date());
        confirm.setConfirmUser(SecurityContextUtil.getCurrentUserName());
        List<ProductionConfirm> confirmList = new ArrayList<ProductionConfirm>();
        confirmList.add(confirm);
        productionOrderConfirmService.doConfirmProduction(confirmList);
        return dialogAjaxDone(navTabId, null, null, null);

    }


    @RequestMapping(value = "viewProductionOperation")
    public String viewProductionOperation(String navTabId, Model model, Long id) {
        model.addAttribute(NAVTABID, navTabId);
        ProductionOperation pop = productionOrderConfirmService.getProductionOperationById(id);
        List<ProductionConfirm> confirmList = productionConfirmDetailService.
                getProductionConfirms(pop.getSiteCode(), pop.getProductionControlNum(), pop.getProcessOperationId());
        model.addAttribute("productionOperation", pop);
        model.addAttribute("confirms", confirmList);
        return "pp/productionConfirm/confirm_detail";

    }
}
